Page({
  data: {
    isSelecting: false,
    weekInfo: "",
    userCourseList: [],
    searchValue: "",
    colorArray: [],
    url:""
  },
  //第一次加载
  onLoad(query) {
    this.myLoadFunction(query);
  },
  myLoadFunction(query){
    this.setData({
      url: getApp().globalData.url
    });
   this.firstLoadData(query);
    dd.stopPullDownRefresh() //刷新完成后停止下拉刷新动效

  },

  onReady() {
    // 页面加载完成
    dd.showToast({
      type: "success",
      content: '加载成功',
      duration: 2000
    })
  },

  //初次加载的时候
  firstLoadData(query) {
    //先处理好当天的日期
    this.setData({
      weekInfo: parseInt(query.weekIndex)+1
    });
    //第一次加载需要请求当前userId的今日课表
    this.setData({
      id: query.ddId
    })
    // this.getRandomColor();
    this.getCourseList();
    
  },

  //发送请求获取数据
  getCourseList() {
    let $this = this;
    dd.httpRequest({
      url: $this.data.url+"/course/getCourseByWeek",
      method: 'GET',
      data: {
        ddId: this.data.id,
        week: this.data.weekInfo
      },
      success: function(res) {
        let len = $this.data.userCourseList.length;
        if(res.data.code == 0){
          $this.$spliceData({ userCourseList: [0, len] })
          for(let j = 1;j <= 7;j++){
            for(let i = 0;i < res.data.data.length;i++){
              if(res.data.data[i][i+1][j].length > 0){
                $this.$spliceData({ userCourseList: [$this.data.userCourseList.length, 0,res.data.data[i][i+1][j][0]] })
              }
            }
          }

          $this.getRandomColor()
          
          for(let i = 0;i < $this.data.userCourseList.length;i++){
            let weekday = 'userCourseList['+i+'].weekday'
            if($this.data.userCourseList[i].weekday == 1){
              // 改变数组内某一属性的值
              $this.setData({
                [weekday]:'一'
              })
            }else if($this.data.userCourseList[i].weekday == 2){
              $this.setData({
                [weekday]:'二'
              })
            }else if($this.data.userCourseList[i].weekday == 3){
              $this.setData({
                [weekday]:'三'
              })
            }else if($this.data.userCourseList[i].weekday == 4){
              $this.setData({
                [weekday]:'四'
              })
            }else if($this.data.userCourseList[i].weekday == 5){
              $this.setData({
                [weekday]:'五'
              })
            }else if($this.data.userCourseList[i].weekday == 6){
              $this.setData({
                [weekday]:'六'
              })
            }else if($this.data.userCourseList[i].weekday == 7){
              $this.setData({
                [weekday]:'日'
              })
            }
          }

        }else{
          $this.$spliceData({ userCourseList: [0, len] })
          console.log($this.data.id)
          dd.showToast({
            type: "exception",
            content: '没有课',
            duration: 2000
          })
        }
      },
      fail: function(res) {
        dd.showToast({
          type: "exception",
          content: '操作失败，请检查网络',
          duration: 2000
        })
      }
    })
  },

  //获取随机颜色
  getRandomColor() {
    let colorArray = [];
    let len = this.data.userCourseList.length;
    for (let i = 0; i < 7; i++) {
      colorArray.push('#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).substr(-6));
    }
    for (let i = 0; i < len; i++) {
      this.$spliceData({ colorArray: [len, 0, colorArray[this.data.userCourseList[i].weekday]] })
    }
  },


})